package com.easycarebn.main.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.easycarebn.main.utils.IResult;
import com.easycarebn.main.utils.Utils;
import com.easycarebn.request.data.AppointmentItem;
import com.squareup.picasso.Picasso;
import com.timbacsi.main.R;

public class AappointmentAdapter extends BaseAdapter {
	ViewHolder holder;
	Context mContext;
	List<AppointmentItem> arr;
	IResult callIResult;

	public AappointmentAdapter(Context mContext, List<AppointmentItem> arr,
			IResult callIResult) {
		this.mContext = mContext;
		this.arr = arr;
		this.callIResult = callIResult;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arr.size();
	}

	public void removeAllItem() {
		for (int i = arr.size() - 1; i >= 0; i--) {
			arr.remove(i);
		}
		notifyDataSetChanged();
	}

	public void removeItem(int id) {
		for (int i = arr.size() - 1; i >= 0; i--) {
			if (arr.get(i).getId() == id) {
				arr.remove(i);
				break;
			}
		}
		notifyDataSetChanged();
	}

	@Override
	public AppointmentItem getItem(int position) {
		// TODO Auto-generated method stub
		return arr.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	public class ViewHolder {
		public TextView drname;
		public TextView phone;
		public TextView email;

		public ImageView icon;

		public Button btnHuy;

	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			// Utilities.log("convert null " + position);
			holder = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.item_appointments, null);
			holder.drname = (TextView) convertView.findViewById(R.id.drname);
			holder.phone = (TextView) convertView.findViewById(R.id.phone);
			holder.email = (TextView) convertView.findViewById(R.id.email);
			holder.btnHuy = (Button) convertView.findViewById(R.id.btnHuy);
			holder.icon = (ImageView) convertView
					.findViewById(R.id.icon);

			convertView.setTag(holder);
		} else {
			// Utilities.log("convert not null " + position);
			holder = (ViewHolder) convertView.getTag();
		}
		AppointmentItem item = getItem(position);
		holder.drname.setText(item.getDoctor().getFullname());
		if (item.getAddress().isEmpty()) {
			Utils.setTextEmpty(holder.phone, (Activity) mContext);
		} else {
			holder.phone.setText(Html.fromHtml(item.getAddress()));
		}

	
		 Picasso.with(mContext).load(item.getDoctor().getAvatarThumb()).placeholder(R.drawable.avata).error(R.drawable.avata).into(holder.icon);

		if (item.getStatus() == -1 || item.getStatus() == -2
				|| item.getStatus() == -3 || item.getStatus() == -4) {
			holder.btnHuy.setVisibility(View.GONE);
		}

		holder.btnHuy.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				callIResult.onDisplay(Activity.RESULT_CANCELED, position + "");
			}
		});

		return convertView;
	}

}
